package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.CommentAdapter;
import com.xgkj.diyiketang.data.CommentInfo;
import com.xgkj.diyiketang.data.ContentInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.CommentListTask;
import com.xgkj.diyiketang.task.ContentDetailTask;
import com.xgkj.diyiketang.task.PublishCommentTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.ClearableEditText;
import com.xgkj.diyiketang.weidget.MyCustomListView;
import com.xgkj.diyiketang.weidget.MyScrollView;

import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/8/1.
 */
public class TieZiDetailActivity extends BaseActivity implements View.OnClickListener{
    private Context mContext;
    private String contentId;
    private TextView titleText;
    private TextView descText;
    private ImageView mImage;
    private int tieziDetailTaskId = -1;
    private ContentInfo contentInfo;

    private Button pinglunBtn;
    private String cmtText;
    private ClearableEditText cmtContant;
    private int commentListTaskId = -1;
    private int publishCmtTaskId = -1;
    private int currentPage = 1;
    private MyCustomListView commentList;
    private CommentAdapter cmtAdapter;
    private MyScrollView mySroll;
    private TextView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tiezi_detail);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            contentId = intent.getStringExtra("ID");
        }
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mySroll = (MyScrollView) findViewById(R.id.detail_scroll);
        mySroll.setOnScrollListener(new MyScrollView.OnScrollListener() {
            @Override
            public void onScroll() {
                getCommentList();
            }
        });
        titleText = (TextView) findViewById(R.id.tiezi_title);
        descText = (TextView) findViewById(R.id.tiezi_desc);
        mImage = (ImageView) findViewById(R.id.tiezi_image);

        pinglunBtn = (Button) findViewById(R.id.fabiaopinglun);
        pinglunBtn.setOnClickListener(this);


        cmtContant = (ClearableEditText) findViewById(R.id.pinglun);
        commentList = (MyCustomListView) findViewById(R.id.comment_list);
        cmtAdapter = new CommentAdapter(mContext, contentId, 1);

        commentList.setAdapter(cmtAdapter);

        getDetail();
    }

    private void getDetail() {
        ContentDetailTask dTask = new ContentDetailTask(contentId);
        dTask.addListener(new DetailTaskListener(mContext));
        tieziDetailTaskId = getEatShowApp().getTaskManager().addTask(dTask);
    }

    private void getCommentList() {
        if (currentPage == 0) {
            commentList.setNoNextPage();
            return;
        }
        CommentListTask cmtListTask = new CommentListTask(10, currentPage, contentId);
        cmtListTask.addListener(new DetailTaskListener(mContext));
        commentListTaskId = getEatShowApp().getTaskManager().addTask(cmtListTask);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fabiaopinglun:
                if (checkInput()) {
                    publishComment();
                    cmtContant.setText(null);
                }

                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        currentPage = 1;
        getCommentList();
    }

    private void publishComment() {
        PublishCommentTask pcTask = new PublishCommentTask(contentId, cmtText, "", "");
        pcTask.addListener(new DetailTaskListener(mContext));
        publishCmtTaskId = getEatShowApp().getTaskManager().addTask(pcTask);
    }

    class DetailTaskListener extends ShowToastTaskEventListener {

        public DetailTaskListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == tieziDetailTaskId){
                contentInfo = (ContentInfo) data;
                titleText.setText(contentInfo.getTitle());
                descText.setText(contentInfo.getContent());
                String img = contentInfo.getImage();
                if (img != null && img.length() > 0) {
                    ImageCache.getInstance(mContext).loadBitmapToView(mImage, img, null);
                }
            }else if (taskId == publishCmtTaskId) {
                Toast.makeText(mContext, R.string.pinglun_success, Toast.LENGTH_SHORT)
                        .show();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(cmtContant.getWindowToken(), 0);
                cmtContant.setText("");

                currentPage = 1;
                getCommentList();
            } else if (taskId == commentListTaskId) {
                if (data != null) {
                    List<CommentInfo> list = (List<CommentInfo>) data;
                    if (currentPage == 1) {
                        cmtAdapter.clearData();
                    }
                    if (list != null && list.size() > 0) {
                        cmtAdapter.updateData(list);
                    }
                    currentPage = result;
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }

    private boolean checkInput() {
        cmtText = cmtContant.getText().toString().trim();

        if (TextUtils.isEmpty(cmtText)) {
            Toast.makeText(mContext, R.string.no_cmt, Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
        return true;
    }
}
